package com.hippo.core.ui.compose.mine

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.HorizontalSpacer
import com.hippo.core.ui.components.OptionButton
import com.hippo.core.ui.components.StateImage
import com.hippo.core.ui.components.StateImageButton
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.theme.ColorBorder
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.NotificationVM
import com.thingclips.smart.sdk.bean.message.MessageBean
import com.thingclips.smart.sdk.bean.message.MessageType

@Composable
fun NotificationScreen(onBack: () -> Unit, vm: NotificationVM = viewModel()) {

    val uiState = vm.uiState

    CommonBody(
        title = stringResource(R.string.notification_1),
        onBack = onBack
    ) {

        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(color = Color.White)
                .paddingBody()
        ) {

            Column(
                modifier = Modifier
                    .padding(top = 42.dp)
                    .background(color = Color.White)
                    .border(0.5.dp, ColorBorder, shape = MaterialTheme.shapes.medium)
                    .paddingBody()
            ) {

                Spacer(Modifier.height(42.dp))

                LazyColumn {

                    items(uiState.notificationList.size) {index ->
                        val item = uiState.notificationList[index]
                        ItemNotification(item)
                        if( index < uiState.notificationList.size-1){
                            HorizontalDivider(thickness = 0.5.dp)
                        }
                    }

                }

            }



            Row {

                Spacer(modifier = Modifier.weight(1f))

                StateImageButton(
                    checked = uiState.messageType == MessageType.MSG_REPORT,
                    defaultImageRes = R.mipmap.notify_1n,
                    checkedImageRes = R.mipmap.notify_1s,
                    modifier = Modifier
                        .size(84.dp),
                    onClick = {
                        vm.getNotificationList(MessageType.MSG_REPORT)
                    })
                Spacer(modifier = Modifier.weight(1f))
                StateImageButton(
                    checked = uiState.messageType == MessageType.MSG_FAMILY,
                    defaultImageRes = R.mipmap.notify_2n,
                    checkedImageRes = R.mipmap.notify_2s,
                    modifier = Modifier
                        .size(84.dp),
                    onClick = {
                        vm.getNotificationList(MessageType.MSG_FAMILY)
                    })
                Spacer(modifier = Modifier.weight(1f))
                StateImageButton(
                    checked = uiState.messageType == MessageType.MSG_NOTIFY,
                    defaultImageRes = R.mipmap.notify_3n,
                    checkedImageRes = R.mipmap.notify_3s,
                    modifier = Modifier
                        .size(84.dp),
                    onClick = {
                        vm.getNotificationList(MessageType.MSG_NOTIFY)
                    })
                Spacer(modifier = Modifier.weight(1f))
            }

        }
    }
}


@Composable
fun ItemNotification(item: MessageBean) {

    Row(modifier = Modifier.padding(vertical = 8.dp)) {

        Image(
            painter = painterResource(id = R.mipmap.notification),
            contentDescription = null,
            modifier = Modifier.size(60.dp)
        )


        Column {

            VerticalSpacer(height = 5.dp)

            Row {

                Text(
                    modifier = Modifier.weight(1f),
                    text = item.msgTypeContent,
                    style = MaterialTheme.typography.titleMedium
                )

                Text(
                    text = item.dateTime,
                    style = MaterialTheme.typography.bodySmall
                )

            }

            VerticalSpacer(height = 10.dp)

            Text(
                text = item.msgContent,
                style = MaterialTheme.typography.bodySmall
            )
        }

    }

}

@Preview(showBackground = true)
@Composable
private fun NotificationScreenPreview() {
    HippoTheme {
        NotificationScreen({})
    }
}